home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / DMO / DMOSample / util.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  5.8 KB  |  202 lines

  1. //------------------------------------------------------------------------------
  2. // File: Util.cpp
  3. //
  4. // Desc: DirectShow sample code - implementation of utility routines.
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "stdafx.h"
  11. #include "dmo.h"
  12. #include "amvideo.h"
  13. #include "util.h"
  14.  
  15. //  Helper to get the important info out of VIDEOINFOHEADER
  16. void GetVideoInfoParameters(
  17.     const VIDEOINFOHEADER *pvih,
  18.     PBYTE pbData,
  19.     DWORD *pdwWidth,
  20.     DWORD *pdwHeight,
  21.     LONG *plStrideInBytes,     //  Add this on to a pointer to a row to get the new row down
  22.     BYTE **ppbTop               //  Return position of first byte of top row of pixels
  23.     )
  24. {
  25.         LONG lStride;
  26.  
  27.     //  biWidth is the stride in pixels for 'normal formats'
  28.     //  Expand to bytes and round up to a multiple of 4
  29.         if (pvih->bmiHeader.biBitCount != 0 &&
  30.             0 == (7 & pvih->bmiHeader.biBitCount)) {
  31.             lStride = (pvih->bmiHeader.biWidth * (pvih->bmiHeader.biBitCount / 8) + 3) & ~3;
  32.         } else {
  33.             lStride = pvih->bmiHeader.biWidth;
  34.         }
  35.  
  36.     //  If rcTarget is empty it means 'the whole image'
  37.     if (IsRectEmpty(&pvih->rcTarget)) {
  38.         *pdwWidth = (DWORD)pvih->bmiHeader.biWidth;
  39.         *pdwHeight = (DWORD)(abs(pvih->bmiHeader.biHeight));
  40.         if (pvih->bmiHeader.biHeight < 0) {
  41.             *plStrideInBytes = lStride;
  42.             *ppbTop           = pbData;
  43.         } else {
  44.             *plStrideInBytes = -lStride;
  45.             *ppbTop = pbData + lStride * (*pdwHeight - 1);
  46.         }
  47.     } else {
  48.         *pdwWidth = (DWORD)(pvih->rcTarget.right - pvih->rcTarget.left);
  49.         *pdwHeight = (DWORD)(pvih->rcTarget.bottom - pvih->rcTarget.top);
  50.         if (pvih->bmiHeader.biHeight < 0) {
  51.             *plStrideInBytes = lStride;
  52.             *ppbTop = pbData +
  53.                      lStride * pvih->rcTarget.top +
  54.                      (pvih->bmiHeader.biBitCount * pvih->rcTarget.left) / 8;
  55.         } else {
  56.             *plStrideInBytes = -lStride;
  57.             *ppbTop = pbData +
  58.                      lStride * (pvih->bmiHeader.biHeight - pvih->rcTarget.top - 1) +
  59.                      (pvih->bmiHeader.biBitCount * pvih->rcTarget.left) / 8;
  60.         }
  61.     }
  62. }
  63.  
  64. //  stuff to draw string
  65. PBYTE TextBitmap(LPCTSTR lpsz, SIZE *pSize)
  66. {
  67.     HDC hdc = CreateCompatibleDC(NULL);
  68.     if (NULL == hdc) {
  69.         return NULL;
  70.     }
  71.     if (!GetTextExtentPoint32(hdc, lpsz, lstrlen(lpsz), pSize))
  72.     {
  73.         return NULL;
  74.     }
  75.     
  76.     // Create our bitmap
  77.     struct {
  78.         BITMAPINFOHEADER bmiHeader;
  79.         DWORD rgbEntries[2];
  80.     } bmi =
  81.     {
  82.         {
  83.             sizeof(BITMAPINFOHEADER),
  84.             pSize->cx,
  85.             pSize->cy,
  86.             1,    
  87.             1,    
  88.             BI_RGB,
  89.             0,
  90.             0,
  91.             0
  92.         },
  93.         {
  94.             0x00000000,
  95.             0xFFFFFFFF
  96.         }
  97.     };
  98.  
  99.     HBITMAP hbm = CreateDIBitmap(hdc, &bmi.bmiHeader, 0, NULL, NULL, 0);
  100.     if (NULL == hbm) {
  101.         DeleteDC(hdc);
  102.         return NULL;
  103.     }
  104.  
  105.     HGDIOBJ hobj = SelectObject(hdc, hbm);
  106.     if (NULL == hobj) {
  107.         DeleteObject(hbm);
  108.         DeleteDC(hdc);
  109.         return NULL;
  110.     }
  111.     PBYTE pbReturn = NULL;
  112.     BOOL bResult = ExtTextOut(hdc, 0, 0, ETO_OPAQUE | ETO_CLIPPED, NULL, lpsz,
  113.                               lstrlen(lpsz), NULL);
  114.     SelectObject(hdc, hobj);
  115.     LONG lLines;
  116.     if (bResult) {
  117.         LONG lWidthInBytes = ((pSize->cx + 31) >> 3) & ~3;
  118.         pbReturn = new BYTE[lWidthInBytes * pSize->cy];
  119.         if (pbReturn) {
  120.             ZeroMemory(pbReturn, lWidthInBytes * pSize->cy);
  121.             lLines = GetDIBits(hdc, hbm, 0, pSize->cy, (PVOID)pbReturn, (BITMAPINFO *)&bmi, DIB_RGB_COLORS);
  122.         }
  123.     }
  124.     DeleteObject(hbm);
  125.     DeleteDC(hdc);
  126.     return pbReturn;
  127. }
  128.  
  129. //  Helper to fill memory
  130. void OurFillRect(const VIDEOINFOHEADER *pvih, PBYTE pbData, WORD wVal)
  131. {
  132.     DWORD dwWidth, dwHeight;
  133.     LONG  lStrideInBytes;
  134.     PBYTE pbTop;
  135.     GetVideoInfoParameters(pvih, pbData, &dwWidth, &dwHeight, &lStrideInBytes, &pbTop);
  136.  
  137.     PBYTE pbPixels = pbTop;
  138.  
  139.     //  For just filling we don't care which way up the bitmap is - we just start at pbData
  140.     for (DWORD dwCount = 0; dwCount < dwHeight; dwCount++) {
  141.         WORD *pWord = (WORD *)pbPixels;
  142.         for (DWORD dwPixel = 0; dwPixel < dwWidth; dwPixel++) {
  143.             pWord[dwPixel] = wVal;    
  144.         }
  145.         //  biWidth is the stride
  146.         pbPixels += lStrideInBytes;
  147.     }
  148. }
  149.  
  150. //  Helper to get some text into memory (note we're ignoring errors!)
  151. void DrawOurText(const VIDEOINFOHEADER * pvih, PBYTE pbData, LPCTSTR szBuffer)
  152. {
  153.  
  154.     //  Copy the data into our real buffer (top lhs)
  155.     DWORD dwWidthTarget, dwHeightTarget;
  156.     LONG lStrideInBytesTarget;
  157.     PBYTE pbTarget;
  158.  
  159.     SIZE size;
  160.  
  161.     //  Get a bit map representing our bits
  162.     PBYTE pbBits = TextBitmap(szBuffer, &size);
  163.     if (NULL == pbBits) {
  164.         return;
  165.     }
  166.     GetVideoInfoParameters(pvih, pbData, &dwWidthTarget, &dwHeightTarget, &lStrideInBytesTarget, &pbTarget);
  167.  
  168.     //  Now copy the data from the DIB section (which is bottom up)
  169.     //  but first check if it's too big
  170.     if (dwWidthTarget >= (DWORD)size.cx && dwHeightTarget > (DWORD)size.cy && size.cx > 0 && size.cy > 0) {
  171.         // PBYTE pbSource = (PBYTE)pbBits;
  172.         DWORD dwSourceStride = ((size.cx + 31) >> 3) & ~3;
  173.  
  174.         for (DWORD dwY = 0; dwY < (DWORD)size.cy; dwY++) {
  175.             WORD *pwTarget = (WORD *)pbTarget;
  176.             PBYTE pbSource = pbBits + dwSourceStride * ((DWORD)size.cy - dwY - 1);
  177.             for (DWORD dwX = 0; dwX < (DWORD)size.cx; dwX++) {
  178.                 if ( !((0x80 >> (dwX & 7)) & pbSource[dwX >> 3]) ) {
  179.                     pwTarget[dwX] = 0x0000; // Black
  180.                 }
  181.             }
  182.             pbTarget += lStrideInBytesTarget;
  183.         }
  184.     }
  185.     delete [] pbBits;
  186. }
  187.  
  188. //  Helper - compares media types - ignoring the advisory fields
  189. bool TypesMatch(const DMO_MEDIA_TYPE *pmt1, const DMO_MEDIA_TYPE *pmt2)
  190. {
  191.     if (pmt1->majortype   == pmt2->majortype &&
  192.         pmt1->subtype     == pmt2->subtype &&
  193.         pmt1->lSampleSize == pmt2->lSampleSize &&
  194.         pmt1->formattype  == pmt2->formattype &&
  195.         pmt1->cbFormat    == pmt2->cbFormat &&
  196.         0 == memcmp(pmt1->pbFormat, pmt2->pbFormat, pmt1->cbFormat)) {
  197.         return true;
  198.     } else {
  199.         return false;
  200.     }
  201. }
  202.